/*
 * Copyright (c) 2018 Hitevision
 */

package tw.com.hitevision.whiteboard.android.util;

import java.lang.reflect.Method;

/**
 * This helper class provides access to {@code android.os.SystemProperties} via reflection.  This
 * technique may not work in future versions of Android.
 */
public class SystemPropertiesUtils {
    /** Value is a {@code String}. */
    public static final String KEY_DEVICE_TYPE = "persist.type.x9";

    /** Value is a {@code String}. */
    public static final String KEY_FRAME_BUFFER_LOCK = "lock.fb";

    /** Value is a {@code String}. */
    public static final String KEY_NOTIFY_EXIT_ON_ANNOTATION = "hht.annotation.close";

    /** Value is a {@code boolean}. */
    public static final String KEY_HAS_SYSTEM_SLEPT = "hht.str.slept";

    /** Value is a {@code boolean}. */
    public static final String KEY_IS_FF_TOUCH = "persist.hht.frogtouch";

    /** Value is a {@code boolean}.  */
    public static final String KEY_SWITCH_UX = "ttyUSB0";

    /** Value is a {@code String}*/
    public static final String KEY_SYSTEM_PLATFORM = "persist.hht.Platforms";

    /** Value is a {@code boolean} */
    public static final String KEY_8386_4k2k = "persist.sys.display.4k2k";

    /** Value is a {@code String}. */
    //used to check the device is 19A or 19B
    public static final String KEY_8386_19A_OR_19B = "persist.hht.Platforms2";

    //当为true时，为19B电容屏，此时屏蔽工具栏中手指识别按钮
    public static final String KEY_19B_CAPACITANCE = "persist.hht.19Bcapacitance";

    public static String getStringValue(String key, String defaultValue) {
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class, String.class);
            return (String) get.invoke(c, key, defaultValue);
        } catch (Exception e) {
            e.printStackTrace();
            return defaultValue;
        }
    }
    public static int getInt(String key, int def) {
        try {
            final Class<?> c = Class.forName("android.os.SystemProperties");
            return (int) c.getMethod("getInt", new Class[]{String.class, int.class})
                    .invoke(c, key, def);
        } catch (Exception e) {
            return def;
        }
    }

    public static boolean getBooleanValue(String key, boolean defaultValue) {
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class, String.class);
            return Boolean.parseBoolean((String) get.invoke(c, key, Boolean.toString(defaultValue)));
        } catch (Exception e) {
            e.printStackTrace();
            return defaultValue;
        }
    }

    public static void setValue(String key, String value) {
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method set = c.getMethod("set", String.class, String.class);
            set.invoke(c, key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void setValue(String key, boolean value) {
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method set = c.getMethod("set", String.class, String.class);
            set.invoke(c, key, Boolean.toString(value));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
